//
//  YMSMainTabBarController.swift
//  YMSWeibo
//
//  Created by 杨茂盛 on 2016/11/12.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit

class YMSMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let hmtabBar = HMTabBar()
        //只读属性不能够直接赋值  --> 间接赋值
        //self.tabBar = hmtabBar
        self.setValue(hmtabBar, forKey: "tabBar")
        
        //监听点击事件
        //有循环引用
        hmtabBar.composeClosure =  {
            //在实际开发中 按钮的点击事件处理的地方 需要留一个查找的标记
            print("撰写按钮被点击啦")
            //print(self!)
            //把自定义的backView作为根视图
            let backView = YMSComposeView()
            UIApplication.shared.keyWindow?.addSubview(backView)
        }
        //添加子控制器
        addChildViewControllers()
    }
    //添加子控制器
    private func addChildViewControllers(){
        addChildViewControllers(Vc: YMSHomeController(), title: "首页", imageName: "tabbar_home",index:0)
        addChildViewControllers(Vc: YMSMessageController(), title: "消息", imageName: "tabbar_message_center",index:1)
        addChildViewControllers(Vc: YMSDiscoverController(), title: "发现", imageName: "tabbar_discover",index:2)
        addChildViewControllers(Vc: YMSProfileController(), title: "我的", imageName: "tabbar_profile",index:3)
    }
    private func addChildViewControllers(Vc:UIViewController,title:String,imageName:String,index:Int){
        Vc.title = title
        //设置默认图片和选中图片
        Vc.tabBarItem.tag = index
        Vc.tabBarItem.image = UIImage(named: imageName)?
            .withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        Vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置默认文字和选中文字
        Vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.black], for: .normal)
        Vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        //设置文字大小
        Vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 14)], for: .normal)
//----------------- ios10.0--------------------------------------------------------------
        //设置tabbar文字的位置
        Vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 0)
        //设置tabarBadgeValue的值
        //Vc.tabBarItem.badgeValue = "10"
         //设置tabarBadgeValue的颜色
        Vc.tabBarItem.badgeColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
        //设置图片的偏移
        //Vc.tabBarItem.imageInsets = UIEdgeInsets(top: 3, left: 0, bottom: -3, right: 0)
//---------------------------------------------------------------------------------------        //设置导航控制器
        let Nav = YMSBaseNavController(rootViewController: Vc)
        //添加到子控制器
        self.addChildViewController(Nav)
    }
    
    //获取被点击的tabbarItem的图片,对它进行形变
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        //print(item.tag) //测试点击的tabBarItem是否是对应的tag
        
        //定义变量 作为下标
        var index = 0
        for subview in tabBar.subviews{
            //判断subview是否是UITabBarButton的子类或者父类
            if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                if index == item.tag{
                //说明获取到了点击的TabBarButton
                    //再遍历这个subview,这里面存的一定是tabBarButton
                    for v in subview.subviews{
                        if v.isKind(of: NSClassFromString("UITabBarSwappableImageView")!) {
                            //print("\(v)") //v就是我们要找的图片
                            
                            //先对它进行缩小
                            v.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
                            //再让它进行还原
                            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 5, options: [], animations: {
                                        v.transform = CGAffineTransform.identity
                            }, completion: { (_) in
                                //print("完成")
                            })
                        }
                    }
                }
                index += 1
            }
        }
    }
}
